# Procedures for creating tex tables

# Arguments:
#   x = matrix of size nr x nc
#   file = file name
#   label = word at the left up corner of the table
#   nomligne = line headers
#   nomcol = columns headers
#   decim = minimum number of decimals
#   blanc = are zeros turned into blacks? default 0 = no
#   append = the file is not scrathed (default: FALSE)



# Tables for simulated data

textable <- function (x, file = "temp", label = "Variable", nomligne,
                      nomcol, decim = 3, blanc = 0, append = FALSE, ...) 
    {

  # Initialisation of the dimensions of tables 
    nc <- ncol(x)
    nr <- nrow(x)

  # Nom du fichier et des marges
    fi <- paste(file, ".tex", sep = "")       

  # Ecriture
     if (!append) cat("", file = fi)
     sl <- "\\\\"
     hl <- "\\hline" 

  # Header    
     cat("\\begin{center}\n", file = fi, sep = "", append = TRUE)
     cat("\\begin{tabular}{", c("l",rep("c",nc)), "}\n ", file = fi, 
                                                      sep = "", append = TRUE) 
     cat(hl,hl, "\n", sep = "", file = fi, append = TRUE)
     cat(label,paste(" &", nomcol, sep = " "), " ", sl, "\n ", file = fi, 
                                                      sep = "", append = TRUE)
     cat(hl, "\n", sep = "", file = fi, append = TRUE)
     
  # Transform into character (element par element pour les decimales!)
    cx <- x
    for (i in 1:nr) {
    	  num <- i-3*trunc((i-1)/3)
        for (j in 1:nc) {
               cx[i,j] <- format(x[i,j], digits = decim)
		   if (nchar(cx[i,j])==1) {cx[i,j]=paste(cx[i,j],".000",sep="")}
		   if (nchar(cx[i,j])==3) {cx[i,j]=paste(cx[i,j],"00",sep="")}
		   if (nchar(cx[i,j])==4) {cx[i,j]=paste(cx[i,j],"0",sep="")}
	  	   if (num==1) { cx[i,j] <- paste("\\textbf{",cx[i,j],"}") }
	  	   if (num==2) { cx[i,j] <- paste("\\textit{",cx[i,j],"}") }
	  	   if (num==3) { cx[i,j] <- paste("[",cx[i,j],"]",sep="") }
	                  } 
     			   }
        if (blanc > 0) cx[abs(x)< 1.e-6] <- "" 

  # Corps
    for (j in 1:nr)
 
    {
       cat(nomligne[j], paste(" &", cx[j,], sep = " ")," ",
             sl, "\n", sep = "", file = fi, append = TRUE)
     }   
  
   # Fin
     cat(hl,hl, "\n", "\\end{tabular}", "\n", "\\end{center}", "\n", 
                  sep = "", file = fi, append = TRUE)

     invisible()
  }


# Tables for real data

textable2 <- function (x, CI, file = "temp", label = "Variable", nomligne,
                       nomcol, decim = 3, blanc = 0, append = FALSE, ...) 
    {

  # Initialisation des dimension des tableaux 
    nc <- ncol(x)
    nr <- nrow(x)

  # Nom du fichier et des marges
    fi <- paste(file, ".tex", sep = "")       

  # Ecriture
     if (!append) cat("", file = fi)
     sl <- "\\\\"
     hl <- "\\hline" 

  # Header    
     cat("\\begin{center}\n", file = fi, sep = "", append = TRUE)
     cat("\\begin{tabular}{", c("l",rep("c",nc)), "}\n ", file = fi, 
                                                      sep = "", append = TRUE) 
     cat(hl,hl, "\n", sep = "", file = fi, append = TRUE)
     cat(label,paste(" &", nomcol, sep = " "), " ", sl, "\n ", file = fi, 
                                                      sep = "", append = TRUE)
     cat(hl, "\n", sep = "", file = fi, append = TRUE)
     
  # Transform into character (element par element pour les decimales!)
    cx  <- x
    CIx <- CI
    for (i in 1:nr) {
    	  num <- i-2*trunc((i-1)/2)
        for (j in 1:nc) {
               cx[i,j] <- format(x[i,j],digits=decim)
               CIx[i,j] <- format(CI[i,j],digits=decim)

		   if ((substr(x[i,j],1,1)=="-")*(nchar(cx[i,j])==5)) { 
					cx[i,j] <- paste(cx[i,j],"0",sep="") }			
		   if ((substr(x[i,j],1,1)=="0")*(nchar(cx[i,j])==4)) { 
					cx[i,j] <- paste(cx[i,j],"0",sep="") }			

		   if ((substr(CIx[i,j],1,1)=="-")*(nchar(CIx[i,j])==5)) { 
					CIx[i,j] <- paste(CIx[i,j],"0",sep="") }			
		   if ((substr(CIx[i,j],1,1)=="0")*(nchar(CIx[i,j])==4)) { 
					CIx[i,j] <- paste(CIx[i,j],"0",sep="") }			

	  	   if (num==2) { 
			cx[i,j] <- paste("[",CIx[2*trunc((i-1)/2)+1,j]," ; ",CIx[2*trunc(i/2),j],"]",sep="")
			}

	                  } 
     			}
        if (blanc > 0) cx[abs(x)+abs(CI)< 1.e-6] <- "" 



  # Corps
    for (j in 1:nr)
 
    {
       cat(nomligne[j], paste(" &", cx[j,], sep = " ")," ",
             sl, "\n", sep = "", file = fi, append = TRUE)
    }   
  
   # Fin
     cat(hl,hl, "\n", "\\end{tabular}", "\n", "\\end{center}", "\n", 
                  sep = "", file = fi, append = TRUE)

     invisible()
  }
